package TCPUpFileImage;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class TcpClient {
    public static void main(String[] args) throws Exception {
        String serverAddress = "localhost";
        int serverPort = 8888;
        // 创建客户端socket
        Socket clientSocket = new Socket(serverAddress, serverPort);
        System.out.println("连接服务端中...");
        // 要上传的文件路径
        String filePath = "C:\\Users\\Administrator\\Desktop\\1\\1.gif";
        FileInputStream fileInputStream = new FileInputStream(filePath);
        OutputStream outputStream = clientSocket.getOutputStream();
        byte[] buffer = new byte[1024 * 8];
        int bytesRead;
        try {
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            System.out.println("文件上传完成！");
        } finally {
            fileInputStream.close();
            outputStream.close();
            clientSocket.close();
        }
    }
}